"""
用户接口
"""


from db import db_handler
from lib import common

def register_interface(username: str,
                       password: str,
                       balance: int = 15000,
                       locked: bool = False) -> tuple:
    """用户注册接口"""
    # 加密密码
    password = common.encrypting_pwd(password)

    user_dict = {
        'username': username,
        'password': password,
        'balance': balance,
        # 用户流水
        'flow': [],
        # 用户购物车
        'shopping_cart': [],
        # 用户账户是否被冻结
        'locked': locked
    }
    db_handler.save(username, user_dict)
    return True, f'{username} 注册成功！'


def login_interface(username: str, password: str) -> tuple:
    """用户登录接口"""

    # 获取用户信息字典
    user_dict = db_handler.select(username)
    # 加密用户输入的密码
    password = common.encrypting_pwd(password)

    if user_dict != None:
        if user_dict.get('username') == username:
            if user_dict.get('password') == password:
                return True, f'{username}登陆成功！'
            else:
                return False, f'密码错误！'
        else:
            return False, f'{username}不存在！'
    else:
        return False, f'{username}不存在！'


def check_balance_interface(username: str) -> int:
    """余额查询接口"""
    user_dict = db_handler.select(username)
    return user_dict.get('balance')